import { Component, ViewChild, ComponentFactoryResolver, Input, OnInit, ViewContainerRef, SimpleChanges } from '@angular/core';
import { EventService } from '../../shared/service/event.service';
import { TabsService } from '../../shared/service/tabs.service';
import { ComItem } from './com-item';
@Component({ selector: 'app-toptab', template: '<section>\n' + '<ng-template #dynamicComponent></ng-template>\n' + '</section>' })
export class TopTabComsComponent implements OnInit {
    @Input() subComs: ComItem[];
    @ViewChild('dynamicComponent', { read: ViewContainerRef }) myRef: ViewContainerRef;
    // tslint:disable-next-line:max-line-length
    constructor(private componentFactoryResolver: ComponentFactoryResolver, private eventService: EventService, public tabService: TabsService) { }
    ngOnInit() {
        // 判断是否已经注册事件过了如果已经注册过了 就不用再注册了
        if (this.eventService.eventEmit.listenerCount('refreshTopTabContainer') > 0) {
            this.eventService.eventEmit.removeListener('refreshTopTabContainer');
        }
        this.eventService.eventEmit.on('refreshTopTabContainer', () => {
            this.refreshComponent();
            if (localStorage.getItem('loginStatus')) {
                localStorage.setItem('isDetailPage', 'true');
            }
        });
    }
    // tslint:disable-next-line:use-life-cycle-interface
    ngOnChanges(changes: SimpleChanges): void { this.myRef.clear(); this.loadComponent(); }
    refreshComponent() {
        const storeTagData = JSON.parse(localStorage.getItem("dataMapTopTabContainer"));
        if (storeTagData !== undefined && storeTagData !== null) {
            setTimeout(() => {
                this.subComs = [];
                localStorage.removeItem("dataMapTopTabContainer");
                // const style = storeTagData.style;
                const cDataObj = storeTagData.currentData;
                if (cDataObj.isGrayBg !== undefined) {
                    localStorage.setItem('refreshGrayBgTag', '' + cDataObj.isGrayBg);
                    this.eventService.eventEmit.emit('refreshGrayBg', 'refreshGrayBg');
                }
                const tags = cDataObj.tags;

                if (tags !== undefined && tags.length > 0) {
                    const len = tags.length;
                    for (let i = 0; i < len; i++) {
                        const tag = tags[i];
                        if (tag.linearLayout === 'body') {
                            this.tabService.getComsDataByStyle(tag.style, callData => {
                                if (callData) {
                                    this.subComs.push(callData);
                                    if (len - 1 === i) {
                                        localStorage.setItem("refreshFooterComsTag", JSON.stringify({ clean: true }));
                                        this.eventService.eventEmit.emit("refreshFooterComsContainer", "refreshFooterComsContainer");
                                    }
                                }
                            }, tag);
                        } else {
                            localStorage.setItem("refreshFooterComsTag", JSON.stringify(tag));
                            this.eventService.eventEmit.emit("refreshFooterComsContainer", "refreshFooterComsContainer");
                        }
                    }
                }
                if (this.subComs !== undefined) {
                    const l = this.subComs.length;
                    this.myRef.clear();
                    for (let j = 0; j < l; j++) {
                        const v = this.subComs[j];
                        const factory = this.componentFactoryResolver.resolveComponentFactory(v.component);
                        const ref = this.myRef.createComponent(factory);
                        ref.instance.tagData = v.tagData;
                        ref.changeDetectorRef.detectChanges();
                    }
                }
            }, 1);
        }
    }
    loadComponent() {
        this.myRef.clear();
        const len = this.subComs.length;
        for (let i = 0; i < len; i++) {
            const v = this.subComs[i];
            const factory = this.componentFactoryResolver.resolveComponentFactory(v.component);
            const ref = this.myRef.createComponent(factory);
            ref.instance.tagData = v.tagData;
        }
    }
}
